package com.v1.video.db.service;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;

import com.v1.video.AppContext;
import com.v1.video.db.dao.OperateTBDao;
import com.v1.video.db.dao.VideoTB;
import com.v1.video.db.dao.VideoTBDao;
import com.v1.video.dbcore.greenrobot.dao.query.QueryBuilder;

public class VideoDB {

	private static VideoDB mInstance;
	private VideoTBDao mVideoTBDao;

	private VideoDB() {
		mVideoTBDao = AppContext.getDaoSession().getVideoTBDao();
	}

	public static VideoDB getInstance() {
		if (mInstance == null) {
			mInstance = new VideoDB();
		}
		return mInstance;
	}
	
	public void insertInTx(List<VideoTB> entities) {
		mVideoTBDao.insertInTx(entities);
	}
	
	public List<VideoTB> getAllData() {
		QueryBuilder<VideoTB> query = mVideoTBDao.queryBuilder();
		return query.build().list();
	}
	
	public List<VideoTB> getSingleData(String cid, int page, int rows) {
		QueryBuilder<VideoTB> query = mVideoTBDao.queryBuilder()
				.where(VideoTBDao.Properties.Cid.eq(cid))
				.orderDesc(VideoTBDao.Properties.Id).limit(rows)
				.offset(page * rows);
		return query.build().list();
	}
	
	/**
	 * @param cid
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<VideoTB> getSingleQuery(String cid, int page, int rows) {
		String sql = "select v." + VideoTBDao.Properties.Id.columnName + ",v."
				+ VideoTBDao.Properties.Vid.columnName + ",v."
				+ VideoTBDao.Properties.Cid.columnName + ",v."
				+ VideoTBDao.Properties.Title.columnName + ",v."
				+ VideoTBDao.Properties.Pic.columnName + ",v."
				+ VideoTBDao.Properties.Duration.columnName + ",o."
				+ OperateTBDao.Properties.Flag.columnName + ",v."
				+ VideoTBDao.Properties.Name.columnName + ",v."
				+ VideoTBDao.Properties.VideoUrl.columnName + ",v."
				+ VideoTBDao.Properties.At.columnName + ",v."
				+ VideoTBDao.Properties.PlayNum.columnName + ",v."
				+ VideoTBDao.Properties.ShareNum.columnName + ",v."
				+ VideoTBDao.Properties.LikeNum.columnName + ",v."
				+ VideoTBDao.Properties.Tid.columnName + ",v."
				+ VideoTBDao.Properties.AdURL.columnName + ",v."
				+ VideoTBDao.Properties.AdShow.columnName + " from "
				+ VideoTBDao.TABLENAME + " as v left join "
				+ OperateTBDao.TABLENAME + " as o on v."
				+ VideoTBDao.Properties.Vid.columnName + "= o."
				+ OperateTBDao.Properties.Vid.columnName + " and v."
				+ VideoTBDao.Properties.Cid.columnName + " =o."
				+ OperateTBDao.Properties.Cid.columnName + " where v."
				+ VideoTBDao.Properties.Cid.columnName + "='" + cid
				+ "' order by v." + VideoTBDao.Properties.Id.columnName
				+ " desc limit " + page * rows + "," + rows;
		Cursor cursor = AppContext.getDaoMaster().getDatabase().rawQuery(sql, null);
		List<VideoTB> VideoLst = null;
		try {
			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				VideoLst = new ArrayList<VideoTB>();
				do {
					VideoTB video = new VideoTB();
					video.setId(cursor.getLong(cursor
							.getColumnIndex(VideoTBDao.Properties.Id.columnName)));
					video.setVid(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.Vid.columnName)));
					video.setCid(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.Cid.columnName)));
					video.setTitle(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.Title.columnName)));
					video.setPic(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.Pic.columnName)));
					video.setDuration(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.Duration.columnName)));
					video.setFlag(cursor.getString(cursor
							.getColumnIndex(OperateTBDao.Properties.Flag.columnName)));
					video.setName(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.Name.columnName)));
					video.setVideoUrl(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.VideoUrl.columnName)));
					video.setAt(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.At.columnName)));
					video.setPlayNum(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.PlayNum.columnName)));
					video.setShareNum(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.ShareNum.columnName)));
					video.setLikeNum(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.LikeNum.columnName)));
					video.setTid(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.Tid.columnName)));
					video.setAdURL(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.AdURL.columnName)));
					video.setAdShow(cursor.getString(cursor
							.getColumnIndex(VideoTBDao.Properties.AdShow.columnName)));
					VideoLst.add(video);
				} while (cursor.moveToNext());
			}
		} finally {
			cursor.close();
		}
		return VideoLst;
	}
	
	public void deleteAll() {
		mVideoTBDao.deleteAll();
	}
}
